package charts.series; 

import charts.series.HasTooltip;
import flash.display.Sprite;
import string.Utils;
import global.Global;
import flash.events.Event;
import flash.events.MouseEvent;
import caurina.transitions.Tweener;
import caurina.transitions.Equations;
import flash.geom.Point;
import flash.net.URLRequest;
//import flash.net.navigateToURL; // must use as is, no function import allowed in haxe
import flash.external.ExternalInterface;
import elements.axis.XAxisLabels;

class Element extends Sprite, implements HasTooltip {
	//
	// for line data
	//
	
	//
	// for line data
	//
	public var _x:Float;
	public var _y:Float;
	
	public var index:Float;
	var tooltip:String;
	var link:String;
	public var is_tip:Bool;
	
	public var line_mask:Sprite;
	var right_axis:Bool;
	
	
	public function new()
	{
		this.right_axis = false;	
	}
	
	public function resize( sc:ScreenCoordsBase ):Void {
		
		var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( this._x, this._y, this.right_axis );
		this.x = p.x;
		this.y = p.y;
	}
	
	//
	// for tooltip closest - return the middle Point
	//
	public function get_mid_Point():flash.geom.Point {
		
		//
		// dots have x, y in the center of the dot
		//
		return new flash.geom.Point( this.x, this.y );
	}
	
	public function get_x(): Float {
		return this._x;
	}

	/**
		* When true, this element is displaying a tooltip
		* and should fade-in, pulse, or become active
		* 
		* override this to show hovered states.
		* 
		* @param	b
		*/
	public function set_tip( b:Bool ):Void {}
	
	
	//
	// if this is put in the Element constructor, it is
	// called multiple times for some reason :-(
	//
	function attach_events():Void {
		
		// weak references so the garbage collector will kill them:
		this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOver, false, 0, true);
		this.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOut, false, 0, true);
	}
	
	public function mouseOver(event:Event):Void {
		this.pulse();
	}
	
	public function pulse():Void {
		// pulse:
		Tweener.addTween(this, {alpha:.5, time:0.4, transition:"linear"} );
		Tweener.addTween(this, {alpha:1,  time:0.4, delay:0.4, onComplete:this.pulse, transition:"linear"});
	}

	public function mouseOut(event:Event):Void {
		// stop the pulse, then fade in
		Tweener.removeTweens(this);
		Tweener.addTween(this, { alpha:1, time:0.4, transition:Equations.easeOutElastic } );
	}
	
	public function set_on_click( s:String ):Void {
		this.link = s;
		this.buttonMode = true;
		this.useHandCursor = true;
		// weak references so the garbage collector will kill it:
		this.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp, false, 0, true);
	}
	
	function mouseUp(event:Event):Void {
		
		if ( this.link.substring(0, 6) == 'trace:' ) {
			// for the test JSON files:
			trace( this.link );
		}
		else if ( this.link.substring(0, 5) == 'http:' )
			this.browse_url( this.link );
		else if ( this.link.substring(0, 6) == 'https:' )
			this.browse_url( this.link );
		else
			ExternalInterface.call( this.link, this.index );
	}
		
	function browse_url( url:String ):Void {
		var req:URLRequest = new URLRequest(this.link);
		try
		{
			flash.net.navigateToURL(req);
		}
		catch (e:Error)
		{
			trace("Error opening link: " + this.link);
		}
	}
	
	public function get_tip_pos():Dynamic {
		return {x:this.x, y:this.y};
	}
	
	
	//
	// this may be overriden by Collection objects
	//
	public function get_tooltip():String {
		return this.tooltip;
	}

	/**
		* Replace #x_label# with the label. This is called
		* after the X Label object has been built (see main.as)
		* 
		* @param	labels
		*/
	public function tooltip_replace_labels( labels:XAxisLabels ):Void {
		
		traces('x label', this._x, labels.get( this._x ));
		this.tooltip =  StringTools.replace(this.tooltip,'#x_label#', labels.get( this._x ) );
	}
	
	/**
		* Mem leaks
		*/
	public function die():Void {
		
		if ( this.line_mask != null ) {
			
			this.line_mask.graphics.clear();
			this.line_mask = null;
		}
	}
}
